class MessageType(object):
    def __init__(self, decs, value, name, group=None, color=None, order=0):
        self.decs = decs
        self.value = value
        self.name = name
        self.order = order or 0
        self.group = group if group else list()
        self.color = color if color else ''

    def __getitem__(self, item):
        if hasattr(self, item):
            return getattr(self, item)
        return None


class MetaStorageInfo(type):

    def __new__(cls, name, base, cdt):
        get_message_info = dict()
        get_message_info_of_name = dict()
        message_list = list()
        for key, val in cdt.items():
            if isinstance(val, (MessageType,)):
                get_message_info[val.value] = val
                message_list.append(val)
                get_message_info_of_name[val.name] = val
        cdt['get_message_info'] = get_message_info
        cdt['get_message_info_of_name'] = get_message_info_of_name
        cdt['message_list'] = message_list
        return super(MetaStorageInfo, cls).__new__(cls, name, base, cdt)


class StorageInfo(metaclass=MetaStorageInfo):
    @classmethod
    def get_message_by_attr(cls, attr):
        """
        返回所有属性的值
        :param attr:
        :return: list
        """
        if not isinstance(attr, str) or attr not in ['name', 'decs', 'value']:
            return []
        return [i[attr] for i in cls.message_list]

    @classmethod
    def get_name_desc_by_value(cls, value):
        """
        根据数据库保存的value返回desc
        :param value:
        :return:
        """
        if type(value) != int:
            return ''

        if value not in cls.get_message_by_attr('value'):
            return ''
        return cls.get_message_info[value].decs

    @classmethod
    def generate_decs_value_map(cls):
        """
        根据数据库保存的value返回desc
        :param value:
        :return:
        """
        return {mess.decs: mess.value for mess in cls.message_list}

    @classmethod
    def generate_name_value_map(cls):
        """
        根据数据库保存的value返回desc
        :param value:
        :return:
        """
        return {mess.name: mess.value for mess in cls.message_list}

    @classmethod
    def generate_name_color_map(cls):
        """
        根据数据库保存的value返回desc
        :param value:
        :return:
        """
        return {mess.value: mess.color for mess in cls.message_list}

    @classmethod
    def get_message_by_desc(cls, desc):
        """
        根据desc获取value
        :param desc:
        :return:
        """
        mess = ''
        for message_obj in cls.message_list:
            if desc == message_obj.decs:
                mess = message_obj
                break

        return mess

    @classmethod
    def get_name_by_value(cls, value):
        """
        根据数据库保存的value返回name
        :param value:
        :return:
        """
        if type(value) != int:
            return ''

        if value not in cls.get_message_by_attr('value'):
            return ''
        return cls.get_message_info[value].name

    @classmethod
    def get_value_by_name(cls, name):
        """
        根据数据库保存的name返回value
        :param name:
        :return:
        """
        if not name:
            return ''

        if name not in cls.get_message_by_attr('name'):
            return ''
        return cls.get_message_info_of_name[name].value

    @classmethod
    def get_group(cls, group_name, attr, all=False):
        """
        获取组的常量信息
        :param group_name: 组名
        :param attr: 属性
        :param all: 返回类型
        :return: 列表
        """
        if not isinstance(attr, str) or attr not in ['name', 'decs', 'value']:
            return []
        return [i[attr] for i in cls.message_list if group_name in i.group] if not all else [i for i in cls.message_list
                                                                                             if group_name in i.group]

    @classmethod
    def get_group_key_val(cls, group_name, key, value):
        """
        获取组的常量信息
        :param group_name: 组名
        :param key: key
        :param value: value
        :return: dict
        """
        if not isinstance(key, str) or key not in ['name', 'decs', 'value']:
            return []
        return {i[key]: i[value] for i in cls.message_list if group_name in i.group}

    @classmethod
    def get_group_attrs(cls, group_name, attrs):
        """
        获取组的常量信息
        :param group_name: 组名
        :param attrs: attrs
        :return: [{},{}]
        """

        for attr in attrs:
            if attr not in ['name', 'decs', 'value']:
                return []

        res = list()
        for i in cls.message_list:
            if group_name in i.group:
                _dict = dict()
                for attr in attrs:
                    _dict[attr] = i[attr]
                res.append(_dict)

        return res

    @classmethod
    def get_dict(cls, key='name', value='value'):
        return {info[key]: info[value] for info in cls.message_list}

    @classmethod
    def get_dict_value_by_name(cls,name:str, key='name', value='value'):
        for info in cls.message_list:
            if info[key] == name:
                return info[value]
        return None

    @classmethod
    def get_obj_by_name(cls, name):
        """
        :param name: name
        :return:
        """
        if name not in cls.get_message_by_attr('name'):
            return ''
        return cls.get_message_info_of_name[name]
